package easier.window.picker;

import static easier.window.picker.CalendarPicker.setCalendarToTimeEnd;
import static easier.window.picker.CalendarPicker.setCalendarToTimeStart;

import android.content.Context;
import easier.window.picker.CalendarPicker.ActionListener;
import java.util.Calendar;
import java.util.Date;

public class CalendarPickerFactory {

    /**
     * 创建一个{@link DateTimePickerView#TYPE_DATE_TIME}类型的时间选择器
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildDateTimePicker( Context context, ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        picker.datePicker.setType( DateTimePickerView.TYPE_DATE_TIME );
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 创建一个{@link DateTimePickerView#TYPE_DATE_HOUR_MINUTE}类型的时间选择器
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildDateHourMinutePicker( Context context, ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        picker.datePicker.setType( DateTimePickerView.TYPE_DATE_HOUR_MINUTE );
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 创建一个{@link DateTimePickerView#TYPE_DATE_TIME}类型的时间选择器
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildYearMonthDayPicker( Context context, ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        picker.datePicker.setType( DateTimePickerView.TYPE_YEAR_MONTH_DAY );
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 创建一个{@link DateTimePickerView#TYPE_YEAR_MONTH_DAY_HOUR_MINUTE}类型的时间选择器
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildYearMonthDayHOurMinutePicker( Context context, ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        picker.datePicker.setType( DateTimePickerView.TYPE_YEAR_MONTH_DAY_HOUR_MINUTE );
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 创建基础的时间选择器
     *
     * @param context        context
     * @param type           显示类型
     * @param start          起始日期
     * @param end            结束日期
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildCalendarPicker( Context context, int type, Calendar start, Calendar end,
        ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        picker.datePicker.setType( type );
        if ( start != null ) {
            picker.datePicker.setStartDate( start );
        }
        if ( end != null ) {
            picker.datePicker.setEndDate( end );
        }
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 自由化配置时间选择器
     *
     * @param context        context
     * @param consumer       配置
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildCalendarPicker( Context context, Consumer consumer, ActionListener actionListener ) {

        CalendarPicker picker = new CalendarPicker();
        picker.createPicker( context );
        if ( consumer != null ) {
            consumer.accept( picker.datePicker );
        }
        picker.actionListener = actionListener;
        return picker;
    }

    /**
     * 用于{@link CalendarPickerFactory#buildCalendarPicker(Context, Consumer, ActionListener)}配置时间选择器
     */
    public interface Consumer {

        void accept( DateTimePickerView view );
    }

    /**
     * 显示年月日,结束日期今天
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildEndTodayPicker( Context context, ActionListener actionListener ) {

        Calendar today = Calendar.getInstance();
        today.setTime( new Date() );
        return buildCalendarPicker( context, DateTimePickerView.TYPE_YEAR_MONTH_DAY, null, today, actionListener );
    }

    /**
     * 显示年月日,结束日期今天
     *
     * @param context        context
     * @param actionListener 回调
     *
     * @return CalendarPicker
     */
    public static CalendarPicker buildStartTodayPicker( Context context, ActionListener actionListener ) {

        Calendar today = Calendar.getInstance();
        today.setTime( new Date() );
        return buildCalendarPicker( context, DateTimePickerView.TYPE_YEAR_MONTH_DAY, today, null, actionListener );
    }

    /**
     * @return 起始日期1970/01/01 00:00:00到2070/12/31 23:59:59
     */
    public static CalendarPicker buildFrom1970after100Year( Context context, ActionListener actionListener ) {

        return buildCalendarPicker( context, new Consumer() {
            @Override
            public void accept( DateTimePickerView view ) {

                Calendar start = Calendar.getInstance();
                Date date = new Date( 0 );
                start.setTime( date );
                setCalendarToTimeStart( start );

                Calendar end = Calendar.getInstance();
                end.setTime( date );
                end.add( Calendar.YEAR, 101 );
                end.add( Calendar.DAY_OF_YEAR, -1 );
                setCalendarToTimeEnd( end );

                view.setType( DateTimePickerView.TYPE_YEAR_MONTH_DAY );
                view.setStartDate( start );
                view.setEndDate( end );

            }
        }, actionListener );
    }

    /**
     * @return 起始日期1970/01/01 00:00:00到今天 23:59:59
     */
    public static CalendarPicker buildFrom1970ToToday( Context context, ActionListener actionListener ) {

        return buildCalendarPicker( context, new Consumer() {
            @Override
            public void accept( DateTimePickerView view ) {

                Calendar start = Calendar.getInstance();
                Date date = new Date( 0 );
                start.setTime( date );
                setCalendarToTimeStart( start );

                Calendar end = Calendar.getInstance();
                end.setTime( new Date() );
                setCalendarToTimeEnd( end );

                view.setType( DateTimePickerView.TYPE_YEAR_MONTH_DAY );
                view.setStartDate( start );
                view.setEndDate( end );

            }
        }, actionListener );
    }

    /**
     * @return 起始日期1970/01/01 00:00:00到今天 23:59:59
     */
    public static CalendarPicker buildFromTodayTo2070( Context context, ActionListener actionListener ) {

        return buildCalendarPicker( context, new Consumer() {
            @Override
            public void accept( DateTimePickerView view ) {

                Calendar start = Calendar.getInstance();
                start.setTime( new Date() );
                setCalendarToTimeStart( start );

                Calendar end = Calendar.getInstance();
                Date date = new Date( 0 );
                end.setTime( date );
                end.add( Calendar.YEAR, 101 );
                end.add( Calendar.DAY_OF_YEAR, -1 );
                setCalendarToTimeEnd( end );

                view.setType( DateTimePickerView.TYPE_YEAR_MONTH_DAY );
                view.setStartDate( start );
                view.setEndDate( end );

            }
        }, actionListener );
    }
}
